package com.quarkus.cloud.controller;

import com.quarkus.cloud.common.util.BaseResponse;
import com.quarkus.cloud.domain.entity.User;
import com.quarkus.cloud.service.UserService;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

import java.util.List;

/**
 * @author zhuhb-a
 */
@Path("/user")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserController {
	@Inject
	UserService userService;

	@GET
	@Path("/search/{id}")
	public User searchById(@PathParam("id") Long id) {
		return userService.findById(id);
	}

	@GET
	@Path("/list")
	public BaseResponse<List<User>> list(@QueryParam("name") String name, @QueryParam("address") String address,
			@QueryParam("age") Integer age, @QueryParam("pageNo") Integer pageNo,
			@QueryParam("pageSize") Integer pageSize) {
		return BaseResponse.success(userService.list(name, address, age, pageNo, pageSize).getRecords());
	}

	@POST
	@Path("/add")
	public BaseResponse<String> addUser(User user) {
		return userService.insert(user) ? BaseResponse.success() : BaseResponse.error();
	}

	@DELETE
	@Path("/delete/{id}")
	public BaseResponse<String> delete(@PathParam("id") Long id) {
		return userService.delete(id) ? BaseResponse.success() : BaseResponse.error();
	}

	@PUT
	@Path("/update")
	public BaseResponse<String> updateUser(User user) {
		return userService.update(user) ? BaseResponse.success() : BaseResponse.error();
	}
}
